package com.cq.sphtech.flow.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cq.sphtech.R;
import com.cq.sphtech.model.FlowInfo;

import java.util.List;

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {

    public List<List<FlowInfo.RecordsBean>> getmList() {
        return mList;
    }

    private List<List<FlowInfo.RecordsBean>> mList;


    private HomeAdapter.OnItemClickListener onItemClickListener;

    private Context mContext;

    public HomeAdapter(Context context, List<List<FlowInfo.RecordsBean>> list) {
        this.mContext = context;
        this.mList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }
    // ① 定义点击回调接口
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    // ② 定义一个设置点击监听器的方法
    public void setOnItemClickListener(HomeAdapter.OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_recycler_view, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.id_years.setText(mList.get(position).get(0).getQuarter().substring(0, 4));
        double count = 0;
        for (FlowInfo.RecordsBean recordsBean : mList.get(position)) {
            if (recordsBean.isFlag()) {
                holder.detail.setVisibility(View.VISIBLE);
            }
            count += Double.valueOf(recordsBean.getVolume_of_mobile_data()).doubleValue();
            if (recordsBean.getQuarter().substring(5).equals("Q1")) {
                holder.t_a.setText(recordsBean.getVolume_of_mobile_data());
            } else if (recordsBean.getQuarter().substring(5).equals("Q2")) {
                holder.t_b.setText(recordsBean.getVolume_of_mobile_data());
            } else if (recordsBean.getQuarter().substring(5).equals("Q3")) {
                holder.t_c.setText(recordsBean.getVolume_of_mobile_data());
            } else if (recordsBean.getQuarter().substring(5).equals("Q4")) {
                holder.t_d.setText(recordsBean.getVolume_of_mobile_data());
            } else {
            }
        }
        holder.detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener!= null){
                    int pos = holder.getLayoutPosition();
                    onItemClickListener.onItemClick(holder.detail, pos);
                }
            }
        });
        holder.id_year_data.setText("总数据消耗: " + count);

    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();

    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView id_years;

        TextView t_a;

        TextView t_b;

        TextView t_c;

        TextView t_d;

        TextView id_year_data;

        ImageView detail;


        public MyViewHolder(View itemView) {
            super(itemView);
            id_years = itemView.findViewById(R.id.id_years);
            t_a = itemView.findViewById(R.id.t_a);
            t_b = itemView.findViewById(R.id.t_b);
            t_c = itemView.findViewById(R.id.t_c);
            t_d = itemView.findViewById(R.id.t_d);
            id_year_data = itemView.findViewById(R.id.id_year_data);
            detail = itemView.findViewById(R.id.detail);
        }
    }
}
